<template>
	<view class="evaluate-page">
		<hsb-business-evaluate-core
			:type="categoryType"
			:errMsg="errMsg"
			:productInfo="productInfo"
			:optionList="selectList"
			:productLoading="productLoading"
			:evaluateLoading="evaluateLoading"
			class="hsb-business-evaluate-core"
			@evaluate="evaluate"
			@reset="resetOptions"
			@change-model="changeModel"
		/>
	</view>
</template>

<script>
import { mapState } from 'vuex'
import { computedSelectData, computedWearData } from '@/components/business/evaluate-poly/utils'

export default {
	computed: {
		...mapState({
			isValidloginState: state => state.user.isValidloginState
		}),
		categoryType () {
			return this.productInfo.typeId === '3' ? 'tv' : 'hsb'
		}
	},
	data() {
		return {
			errMsg: '',
			productId: '',
			productInfo: {},
			selectBackupList: [],
			selectIds: [],
			selectList: [],
			productLoading: true,
			evaluateLoading: false
		}
	},
	methods: {
		/*
		 * 估价选项
		*/
		async getProductParams () {
			const [res] = await this.$apis.getEvaluateParam({
				productId: this.productId,
				version: '1'
			})
			if (res._errCode === '0') {
				// res._data.itemList[2].questions[0].question[0].course.step = [
				// 	{desc: "", name: "外观完好，无任何使用痕迹", order: "1", pic: ["163886188198213.png", "16388618187461.png", "16388617974389.png"]}
				// ]
				// res._data.itemList[2].questions[0].question[2].course.step = [
				// 	{desc: "", name: "外观完好，无任何使用痕迹", order: "1", pic: ["16388617974389.png", "16388618187461.png", "163886188198213.png"]}
				// ]
				this.productInfo = res._data || {}
				const selectList = res._data.itemList || []
				// 答案项联动选项
				const defaultOptionList = this.productInfo.defaultOptionList || []
				// 备份选项初始数据
				this.selectBackupList = computedSelectData(JSON.parse(JSON.stringify(selectList)), defaultOptionList)
				this.selectList = computedSelectData(selectList, defaultOptionList, this.selectIds)
			} else {
				this.errMsg = res._errStr || '暂无估价选项'
			}
			this.productLoading = false
		},
		/*
		 * 选项估价
		*/
		async evaluate ({ selectData = [], prices = 0 }) {
			const selects = []
			selectData.forEach(item => {
				item.questions.forEach(child => {
					child.question.forEach(v => {
						if (v.selected) {
							selects.push(v.id)
						}
					})
				})
			})
			const params = {
				productId: this.productInfo.productId,
				select: JSON.stringify(selects),
				version: '1'
			}
			this.evaluateLoading = true
			const [res] = await this.$apis.evaluate(params)
			this.evaluateLoading = false
			if (res._errCode === '0') {
				const evaluateInfo = res._data || {}
				if (!evaluateInfo.evaluateid) {
					return uni.showToast({
						title: '估价失败，获取估价信息失败',
						icon: 'none'
					})
				}
				uni.navigateTo({
					url: `/pages/create/index?evaluateId=${evaluateInfo.evaluateid}&productId=${params.productId}`
				})
			} else {
				uni.showToast({
					title: res._errStr || '估价失败',
					icon: 'none'
				})
			}
		},
		/*
		 * 重置选项
		*/
		resetOptions () {
			if (this.evaluateLoading) return
			this.selectList = []
			this.$nextTick(() => {
				this.selectList = JSON.parse(JSON.stringify(this.selectBackupList))
			})
		},
		/*
		 * 更换机型
		*/
		changeModel () {
			const routers = getCurrentPages()
			const prevRouter = routers[routers.length - 2]
			if (prevRouter && prevRouter.route === 'pages/products/index') {
				return uni.navigateBack()
			}
			uni.navigateTo({
				url: '/pages/products/index'
			})
		}
	},
	onLoad (options) {
		this.productId = options.productId || ''
		// #ifdef MP-WEIXIN || MP-ALIPAY
		if (!this.isValidloginState) {
			this.$store.dispatch('user/login', {})
		}
		// #endif
		this.getProductParams()
	}
}
</script>

<style lang="scss" scoped>
.evaluate-page {
	display: flex;
	flex-direction: column;
	height: 100%;
	background-color: #F7F7F7;
	/* #ifdef MP-WEIXIN */
	hsb-business-evaluate-core {
		height: 100%;
	}
	/* #endif */
	/* #ifdef MP-TOUTIAO || MP-QQ || MP-BAIDU || MP-KUAISHOU */
	.hsb-business-evaluate-core {
		height: 100%;
	}
	/* #endif */
}
</style>
